//
//  WQOAuthController.m
//  sanwuWeiboOC
//
//  Created by 王先强 on 15/12/8.
//  Copyright © 2015年 王先强. All rights reserved.
//

#import "WQOAuthController.h"
#import "WQNetTool.h"
#import "WQAccount.h"
@interface WQOAuthController ()<UIWebViewDelegate>

@property (nonatomic, weak) UIWebView *webView;
@end

@implementation WQOAuthController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self settingUpOAuthView];
//    WQLog(@"%@",[WQNetTool shareNetTool].oauthURLRequest);
    [self.webView loadRequest:[WQNetTool shareNetTool].oauthURLRequest];
}
#pragma mark - /************** UIWebViewDelegate代理方法 **************/

-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
    NSString *hostStr = @"www.baidu.com";
    NSString *subStr = @"code=";
    
    WQLog(@"%@",request.URL);
    if (![request.URL.host isEqual:hostStr]) {
        return YES;
    }
    
    if (![request.URL.query hasPrefix:subStr]) {
        WQLog(@"用户点击取消了，跳转到回调主页")
        return YES;
    }
    
    NSString *code = [request.URL.query substringFromIndex:subStr.length];
    WQLog(@"用户点击了授权，获取授权码");
    WQLog(@"%@",code);
    [[WQNetTool shareNetTool] loadAccessTokenWithCode:code finishedBlock:^(id success, NSError *error) {
        if (error != nil) {
            WQLog(@"%@",error);
            return;
        }
//        WQLog(@"%@",[success class]);
        WQAccount *account = [WQAccount accountWithDict:success];
//        WQLog(@"%@",account);
        
        //加载用户信息
        
        [[WQNetTool shareNetTool] loadUserInfoWithAccount:account finishedBlock:^(id success, NSError *failure) {
            
            if (error != nil) {
                WQLog(@"%@",error);
                return;
            }
//            WQLog(@"%@",success);
            account.avatar_large = success[@"avatar_large"];
            account.screen_name = success[@"screen_name"];
            
            NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"account.arche"];
            WQLog(@"%@",path);
            [NSKeyedArchiver archiveRootObject:account toFile:path];
        }];
        
    }];
    return NO;
}



#pragma mark - /************** 基本设置 **************/
-(void)loadView{
    [super loadView];
    UIWebView *webView = [[UIWebView alloc] init];
    webView.delegate = self;
    self.view = webView;
    self.webView = webView;
    
}
-(void)settingUpOAuthView{
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"关闭" style:UIBarButtonItemStylePlain target:self action:@selector(close)];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"自动填充" style:UIBarButtonItemStylePlain target:self action:@selector(addAccess)];
    
}
-(void)close{
    [self dismissViewControllerAnimated:YES completion:nil];
}
-(void)addAccess{
    NSMutableString *js = [NSMutableString stringWithString:@"document.getElementById('userId').value = '18580172452';"];
    [js appendString:@"document.getElementById('passwd').value = '18580172452qqqq';"];
    [self.webView stringByEvaluatingJavaScriptFromString:js];
}

@end
